{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        <h5>添加消息</h5>
    </div>
    <div class="ibox-content layui-form">
        <div class="layui-row layui-col-space10 layui-form-item ">
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">标题：</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" placeholder="消息标题" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">接收端：</label>
                <div class="layui-input-block">
                    <select name="to" lay-verify="required" lay-search="" lay-filter="obj">
                        <option value="">请选择接收端</option>
                        <option  value="1">用户端</option>
                        <option  value="2">商户端</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">推送对象：</label>
                <div class="layui-input-block">
                    <select name="obj" lay-verify="required" lay-search="" lay-filter="obj">
                        <option value="">请选择推送对象</option>
                        <option  value="1">全体成员</option>
                        <option  value="2">指定成员</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" id="user" style="display: none">
                <label class="layui-form-label">用户：</label>
                <div class="layui-input-block">
                    <select name="user" lay-verify="" lay-search="" id="user-select" >
                        <option value="">请选择用户</option>
                        {if !empty($user)}
                        {foreach name="user" item="vo"}
                        <option  value="{$vo.user_id}">{$vo.user_username}</option>
                        {/foreach}
                        {/if}
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" id="shop" style="display: none">
                <label class="layui-form-label">商户：</label>
                <div class="layui-input-block">
                    <select name="shop" lay-verify="" lay-search="" id="shop-select">
                        <option value="">请选择商户</option>
                        {if !empty($shop)}
                        {foreach name="shop" item="vo"}
                        <option  value="{$vo.shop_id}">{$vo.shop_name}</option>
                        {/foreach}
                        {/if}
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <label class="layui-form-label">消息类型：</label>
                <div class="layui-input-block">
                    <select name="type" lay-verify="required" lay-search="" lay-filter="type">
                        <option value="">请选择消息类型</option>
                        <option  value="1">系统消息</option>
                        <option  value="4">活动消息</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" id="sys" style="display: none">
                <label class="layui-form-label">内容：</label>
                <div class="layui-input-block">
                    <textarea name="content" placeholder="消息内容" id="content1" lay-verify="required" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8" id="wang" style="display: none">
                <label class="layui-form-label">内容：</label>
                <div class="layui-input-block" id="LAY_editor" lay-verify="editor"></div>
            </div>
            <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>

    var editor = wk.wang({elem:'#LAY_editor'});
    layui.use(['form'], function() {
        var form = layui.form;

        form.on('select(type)', function(data){
            if(data.value == 1){
                $('#sys').show();
                $('#wang').hide();
                $('#LAY_editor').attr('lay-verify','');
                $('#content1').attr('lay-verify','required');
            }else if(data.value == 4){
                $('#wang').show();
                $('#sys').hide();
                $('#content1').attr('lay-verify','');
                $('#LAY_editor').attr('lay-verify','editor');
            }else{
                $('#sys').hide();
                $('#wang').hide();
                $('#content1').attr('lay-verify','required');
                $('#LAY_editor').attr('lay-verify','editor');
            }
        });
        form.on('select(obj)', function(data){
            var to = $("select[name=to]").val();
            var obj = $("select[name=obj]").val();
            if(obj == 2 && to == 1){
                $('#user').show();
                $('#shop').hide();
                $('#user-select').attr('lay-verify','required');
                $('#shop-select').attr('lay-verify','');
            }else if(obj == 2 && to == 2){
                $('#user').hide();
                $('#shop').show();
                $('#user-select').attr('lay-verify','');
                $('#shop-select').attr('lay-verify','required');
            }else{
                $('#user').hide();
                $('#shop').hide();
                $('#user-select').attr('lay-verify','');
                $('#shop-select').attr('lay-verify','');
            }
        });

        form.verify({
            editor:function(value,item){
                if(editor.txt.html() == '<p><br></p>'){
                    return '请填写内容';
                }
            }
        });
        form.on('submit(component-form-element)', function (data) {
            data.field.html = editor.txt.html();//wangEditor
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            $.ajax({
                url:"{:url('messageAdd')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        wk.success(res.msg,"{:url('message/index')}")
                    } else {
                        wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                    }
                }
            })
        });
    });
</script>
</body>
</html>